package com.lock.immersive.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lock.immersive.exception.BusinessException;
import com.lock.immersive.mapper.ProductItemMapper;
import com.lock.immersive.mapper.TuanMapper;
import com.lock.immersive.mapper.UserMapper;
import com.lock.immersive.mapper.UserTuanMapper;
import com.lock.immersive.pojo.domain.ProductItem;
import com.lock.immersive.pojo.domain.Tuan;
import com.lock.immersive.pojo.domain.User;
import com.lock.immersive.pojo.domain.UserTuan;
import com.lock.immersive.pojo.dto.TuanDto;
import com.lock.immersive.pojo.dto.UserTuanDto;
import com.lock.immersive.pojo.enums.Identity;
import com.lock.immersive.pojo.enums.Size;
import com.lock.immersive.pojo.enums.TuanProcess;
import com.lock.immersive.pojo.vo.ProductItemVo;
import com.lock.immersive.service.TuanService;
import com.lock.immersive.utils.ErrorCode;
import com.lock.immersive.utils.JWT;
import com.lock.immersive.utils.ResponseApi;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author zhao
 * @description 针对表【tuan】的数据库操作Service实现
 * @createDate 2023-07-02 00:04:03
 */
@Service
public class TuanServiceImpl extends ServiceImpl<TuanMapper, Tuan>
        implements TuanService {
    @Resource
    public UserMapper userMapper;
    @Resource
    public TuanMapper tuanMapper;
    @Resource
    public UserTuanMapper userTuanMapper;
    @Resource
    public ProductItemMapper productItemMapper;

    @Override
    public ResponseApi<Object> createTuan(TuanDto tuanDto, HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = JWT.getTokenId(token);
        User user = userMapper.selectById(userId);
        Tuan tuan = new Tuan();
        BeanUtils.copyProperties(tuanDto, tuan);
        tuan.setMerchantId(user.getMerchantId());
        int insert = tuanMapper.insert(tuan);
        if (insert > 0) {
            return ResponseApi.success();
        } else {
            throw new BusinessException(ErrorCode.INSERT_ERROR);
        }
    }

    @Override
    public ResponseApi<Object> updateTuan(Tuan tuan, HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = JWT.getTokenId(token);
        User user = userMapper.selectById(userId);
        int i = tuanMapper.updateById(tuan);
        if (i > 0) {
            return ResponseApi.success();
        } else {
            throw new BusinessException(ErrorCode.UPDATE_ERROR);
        }
    }


    @Override
    public ResponseApi<Integer> joinTuan(UserTuanDto userTuanDto, HttpServletRequest request) {
        String userId = JWT.getTokenId(request.getHeader("token"));
        Integer tuanId = userTuanDto.getTuanId();
        Tuan tuan = tuanMapper.selectById(tuanId);
        if (!tuan.getTuanProcess().equals(TuanProcess.YIXIANGJIN) && !tuan.getTuanProcess().equals(TuanProcess.DINGJIN)) {
            throw new BusinessException(ErrorCode.CANNOT_JOIN);
        }
        Integer merchantId = tuan.getMerchantId();
        UserTuan userTuan = new UserTuan();
        BeanUtils.copyProperties(userTuanDto, userTuan);
        userTuan.setMerchantId(merchantId);
        userTuan.setUserId(Integer.valueOf(userId));
        int insert = userTuanMapper.insert(userTuan);
        return ResponseApi.success(insert);
    }

    @Override
    public ResponseApi<ProductItemVo> getProductById(Integer productId, HttpServletRequest request) {
        String userId = JWT.getTokenId(request.getHeader("token"));
        if (userId == null) {
            List<Tuan> tuans = tuanMapper.selectList(new QueryWrapper<Tuan>().eq("product_id", productId));
            List<ProductItem> productItemList = productItemMapper.selectList(new QueryWrapper<ProductItem>().eq("product_id", productId));
            ProductItemVo productItemVo = new ProductItemVo();
            productItemVo.setProductItemList(productItemList);
            productItemVo.setTuanList(tuans);
            productItemVo.setSizeList(Arrays.asList(Size.values()));
            return ResponseApi.success(productItemVo);
        }
        UserTuan userTuan = userTuanMapper.selectOne(new QueryWrapper<UserTuan>().eq("user_id", userId).eq("product_id", productId));
        List<Tuan> tuans = tuanMapper.selectList(new QueryWrapper<Tuan>().eq("product_id", productId));
        List<ProductItem> productItemList = productItemMapper.selectList(new QueryWrapper<ProductItem>().eq("product_id", productId));
        tuans.forEach(tuan -> {
            if (tuan.getTuanId().equals(userTuan.getTuanId())) {
                tuan.setIsJoined(true);
            }
        });
        productItemList.forEach(productItem -> {
            if (productItem.getItemId().equals(userTuan.getProductItemId())) {
                productItem.setIsJoined(true);
            }
        });
        ProductItemVo productItemVo = new ProductItemVo();
        productItemVo.setProductItemList(productItemList);
        productItemVo.setTuanList(tuans);
        productItemVo.setSizeList(Arrays.asList(Size.values()));
        return ResponseApi.success(productItemVo);
    }


}




